const CommomUtil = require('../utils/CommomUtil');
const SessionUtil = require('../utils/SessionUtil');

const user_dao = require('../dao/user_dao');
const dynamic_dao = require('../dao/dynamic_dao');
const redis_session = require('../config').redis_user;

/** 发表动态 */
module.exports.pub_post = pub_post;
async function pub_post(ctx) {
    let { ty_id, ty_s_id, post_content, post_img, music_id } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.isEmpty(ty_id) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(music_id, 32) || CommomUtil.inValid(post_content, 1000)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (post_img.length > 200) {
        resData['ret'] = false;
        resData['msg'] = '图片过多!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let [flag, msg] = await dynamic_dao.publish_post(music_id, post_content, CommomUtil.isEmpty(post_img) ? null : post_img, ty_id);
    resData['ret'] = flag;
    resData['msg'] = msg;

    return ctx.body = resData;
}

/** 删除动态 */
module.exports.del_post = del_post;
async function del_post(ctx) {
    let { ty_id, ty_s_id, post_id } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.isEmpty(ty_id) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(post_id, 32)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let [del_flag, msg] = await dynamic_dao.del_post(post_id, ty_id);
    resData['ret'] = del_flag;
    resData['msg'] = msg;
    return ctx.body = resData;
}

/** 获取动态列表  */
module.exports.query_posts_list = query_posts_list;
async function query_posts_list(ctx) {
    let { ty_id, begin, plen } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(begin) || CommomUtil.isEmpty(plen)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(begin) || CommomUtil.inValidNumber(plen, 50)) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }

    let [user_name, user_img] = await user_dao.get_user_name_and_img(ty_id);
    if (user_name == null || user_img == null) {
        resData['ret'] = false;
        resData['msg'] = '数据异常!';
        return ctx.body = resData;
    }

    let post_tatol = await dynamic_dao.get_posts_total(ty_id);
    if (post_tatol == 0) {
        resData['post_list'] = [];
        resData['post_tatol'] = post_tatol;
        resData['ret'] = false;
        resData['msg'] = '数据异常!';
        return ctx.body = resData;
    }

    let post_list = await dynamic_dao.get_posts_list(ty_id, begin, plen);
    for (let post of post_list) {
        post['user_id'] = ty_id;
        post['user_name'] = user_name;
        post['user_img'] = user_img;
    }
    resData['post_list'] = post_list;
    resData['post_tatol'] = post_tatol;
    resData['ret'] = true;
    resData['msg'] = '获取成功!';
    return ctx.body = resData;
}

